<template>
  <scroll-view class="imgs" :refresher-triggered="triggered" refresher-enabled scroll-y @scroll="imgsScroll"
    :scroll-top="top" @scrolltolower="getImgList(id, num)" @refresherrefresh="onRefresh">
    <view class="list">
      <view class="item" v-for="(item, index) of imgList" :key="index">
        <easy-loadimage @tap="viewImg(item)" mode="aspectFill" :scroll-top="oldTop" :image-src="item"></easy-loadimage>
        <view @tap="likeImg(item)" class="like">
          <uni-icons type="heart-filled" v-if="likeImgs.includes(item)" color='red' size="25"></uni-icons>
          <uni-icons type="heart" v-else size="25" color="#fffef9"></uni-icons>
        </view>
      </view>
    </view>
    <view class="loading" v-show="!triggered">
      <uni-load-more iconType="circle" status="loading" />
    </view>
  </scroll-view>
  <view class="tabbar" v-if="bottom" ref="ani"
    :style="{ Transform: `translateY(${move}px)`, opacity: `${move == 0 ? 1 : 0}` }">
    <view class="item">
      <view class="bo" @tap="goHome" hover-class="hover-view" custom-class="transition" hover-stay-time="200">
        <image class="logoImg" :src="homeImg" mode="" />
        <text style="color: #ff9671;">首页</text>
      </view>
      <view class="bo" @tap="goClassify" hover-class="hover-view" custom-class="transition" hover-stay-time="200">
        <image class="logoImg" :src="classImg" mode="" />
        <text style="color: #2c73d2;">分类</text>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import classImg from '@/static/images/class.png'
import homeImg from '@/static/images/home.png'
import { onMounted, onBeforeUnmount } from 'vue'
import tabbar from './tabbar'
import datalist from './datalist'
import easyLoadimage from '@/components/easy-loadimage/easy-loadimage.vue'
import { useLikeStore } from '@/stores'
import { storeToRefs } from 'pinia'
const { likeImgs, userName } = storeToRefs(useLikeStore())
const likeStore = useLikeStore()
const { goHome, goClassify } = tabbar()
const { top, oldTop, move, imgList, triggered, imgsScroll, getImgList, viewImg } = datalist()
const props = defineProps({
  id: {
    type: String,
    default: 'sjpic'
  },
  num: {
    type: Number,
    default: 15
  },
  all: {
    type: Boolean,
    default: false
  },
  bottom: {
    type: Boolean,
    default: true
  },
  Other: {
    type: Boolean,
    default: false
  }
})

const likeImg = (item: any) => {
  if (!userName.value) {
    uni.showToast({
      title: '请先登录',
      icon: 'none'
    })
  } else {
    const exit = likeImgs.value.includes(item)
    if (!exit) {
      likeStore.addImg(item)
    } else {
      likeStore.delImg(item)
    }
  }
}
const onRefresh = () => {
  triggered.value = true

  imgList.value = []
  if (props.Other == false) {
    getImgList(props.id, props.num, props.all)
  }
}
onMounted(() => {
  if (props.Other == false) {
    getImgList(props.id, props.num, props.all)
  }
})

onBeforeUnmount(() => {

})
</script>

<style scoped lang="scss">
.imgs {
  width: 100%;
  box-sizing: border-box;
  padding: 0 5rpx;

  .list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5rpx;

    .item {
      width: 100%;
      border-radius: 10rpx;
      height: 600rpx;
      overflow: hidden;
      position: relative;
      box-shadow: 0 0 4rpx #8080805a;

      .like {
        position: absolute;
        z-index: 5;
        bottom: 10rpx;
        right: 10rpx;
        transition: all 0.2s ease-in-out;
      }


      :deep(.easy-loadimage) {
        width: 100%;
      }

      :deep(.origin-img) {
        border-radius: 10rpx;
      }

      /* mode为widthFix即图片高度自适应时要设置占位图默认高度 */
      :deep(.loadfail-img),
      :deep(.loading-img) {
        height: 600rpx;
      }
    }
  }

  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200rpx;
  }
}

.tabbar {
  height: 100rpx;
  width: 100%;
  z-index: 100;
  width: 100%;
  position: fixed;
  bottom: 0;
  left: 0;
  transition: all 0.4s ease-in-out;

  .item {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
    height: 100rpx;



    .bo {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: 24rpx;
      color: black;
      font-weight: bold;
      background-color: rgba(255, 255, 255, 0.603);
      gap: 5px;

      .logoImg {
        width: 40rpx;
        height: 40rpx;
      }
    }
  }

}
</style>
